<?php

namespace App\Http\Controllers\Admin;

use App\Models\Adminer;
use App\Models\Shop;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class AjaxController extends Controller
{
    protected $message, $code;

    public function __construct($message = "", $code = 200)
    {
        $this->message = $message;
        $this->code = $code;
    }

    public function getResponseData()
    {
        $data['code'] = $this->code;
        $data['message'] = $this->message;
        $data['data'] = [];
        return $data;
    }

    public function getStatusCode($statusCode = 500)
    {
        $statusCode = substr($this->code, 0, 3);
        return $statusCode;
    }

    public function apiResponse( $data = [], $code = 200, $msg = '',$other = []){
        if(empty($other)){
            return response()->json([
                'code'      => $code,
                'message'   => $msg,
                'data'      => $data ?? [],
            ]);
        }
        return response()->json([
            'code'      => $code,
            'message'   => $msg,
            'data'      => $data ?? [],
            $other[0]   => $other[1]
        ]);
    }


    /* 获取部门列表 */
    public function department(){

        $data = DB::table('user_department')->select('id','department_name')->get();

        return $this->apiResponse($data);
    }


    /* 获取职位列表 */
    public function position($fcode){

        $data = DB::table('user_position')->where('department_id',$fcode)->select('id','position_name')->get();

        return $this->apiResponse($data);
    }



    /* 获取归属区域列表 */
    public function region(){

        $data = DB::table('region')->select('id','name')->get();

        return $this->apiResponse($data);
    }

    /* 获取省JSON */
    public function province(){

        $data = DB::table('province')->select('code','name')->get();

        return $this->apiResponse($data);
    }

    /* 获取市JSON */
    public function city($fcode){

        $data = DB::table('city')->where('fcode',$fcode)->select('code','name')->get();

        return $this->apiResponse($data);
    }

    /* 获取区JSON */
    public function area($fcode){

        $data = DB::table('area')->where('fcode',$fcode)->select('code','name')->get();

        return $this->apiResponse($data);
    }

    /* 获取销售经理用户 */
    public function manager(){

        $data = DB::table('adminer')->where('role',Adminer::ManagerRole)->select('id','realname','username','phone')->get();

        return $this->apiResponse($data);
    }

    /* 根据省获取归属区域信息 */
    public function regionByProvince($code){

        $data = DB::table('region')
            ->leftjoin('adminer', 'region.id', '=', 'adminer.region')
            ->whereRaw("FIND_IN_SET(?,province_id)",[$code])
            ->select('region.id','region.name', 'adminer.id as manager', 'adminer.realname as manager_name')
            ->first();

        return $this->apiResponse($data);

    }

    /* 统计各个省份店铺数量 */
    public function shopsByRegion($region){
        $data = DB::table('shop')
            ->selectRaw("level as name,count(id) as value,left(area, LOCATE(',',area)-1) as province_id")
            ->whereNotNull('level')
            ->where('region',$region)
            ->where('status',Shop::StatusSuccess)
            ->groupBy("level",DB::raw("left(area, LOCATE(',',area)-1)"))
            ->get()
            ->groupBy('name');
        return $this->apiResponse($data);
    }

}
